perm filename LIBRAR.LSP[F81,JMC] blob
sn#629507 filedate 1981-12-16 generic text, type C, neo UTF8
COMMENT ā VALID 00002 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 librar.lsp[f81,jmc] library of useful lisp functions
C00005 ENDMK
Cā;
;;; librar.lsp[f81,jmc] library of useful lisp functions
;;; These IF macros should be initialized with maclisp, but they seem to lose
;;; sometimes, so here they are. 1981 december.
(defun if macro (l)(cons 'cond (ifxxx1 (cdr l))))
(defun ifxxx1 (u) (cond ((null u) nil) ((null (cdr u)) (list (list t (car u))))
(t (cons (list (car u) (cadr u)) (ifxxx1 (cddr u))))))
;;; Shuffles a list in random order by assigning each element randomly to
;;; one of two output lists w1 and w2 and then appending the results
;;; of shuffling w1 and w2. It assigns equal probability to each of
;;; the n! permutations of u. It only terminates with probability one,
;;; since if the random number generator gave always 0 or always 1 it
;;; wouldn't terminate.
(defun shuffle (u)
(if (or (null u) (null (cdr u)))
u
(shuffle1 u nil nil)))
(defun shuffle1 (u w1 w2)
(if (null u)
(append (shuffle w1) (shuffle w2))
(= 0 (random 2))
(shuffle1 (cdr u) (cons (car u) w1) w2)
(shuffle1 (cdr u) w1 (cons (car u) w2))
))